#include"TcpServer.hpp"
#include"Logger.hpp"
#include"Http.hpp"

void Usage(std::string proc)
{
    std::cerr << "Usage: "<< proc << " serverport" <<std::endl; 
}

std::string Testhttp(std::string& requestsstr)
{
    //处理请求，并返回应答
    //此处没有处理应答
    std::cout <<"#################################" <<std::endl;
    std::cout << requestsstr << std::endl;
    std::cout <<"#################################" <<std::endl;

    std::string respoce = "HTTP/1.1 200 OK\r\n\r\n";

    respoce += "<!DOCTYPE html>\r\n<html>\r\n<body>\r\n<h1>hello world</h1>\r\n</body>\r\n</html>";

    return respoce;
}

//上层实现方法
HttpResponce Login(HttpRequest&req)
{
    LOG(LogLevel::DEBUG) << "--------------Login---------------: " << req.Body();
    return HttpResponce();
}
HttpResponce Register(HttpRequest&req)
{
    LOG(LogLevel::DEBUG) << "--------------Register---------------: "<< req.Body();
     return HttpResponce();
}
HttpResponce Search(HttpRequest&req)
{
    LOG(LogLevel::DEBUG) << "--------------Search---------------: "<< req.Body();
     return HttpResponce();
}

//./server port
int main(int argc , char* argv[])
{
    if(argc !=2)
    {
        Usage("argv[0]");
        return 0;
    }

    EnableConsoleLogStrategy();
    // EnableFileLogStrategy();

    uint16_t port = std::stoi(argv[1]);

    //Http协议
    std::unique_ptr<Http> http = std::make_unique<Http>();

    //将上层放入加入Http 中的unordered_map 中
    http->Register("/login" ,Login);
    http->Register("/register" ,Register);
    http->Register("/search" ,Search);

    //网络层面，从网络中获取数据
    std::unique_ptr<TcpServer> svr = std::make_unique<TcpServer>(port, [&http](std::string& requeststr)->std::string
    {
        return http->HandlerRequest(requeststr);
    });
    svr->Run();
    return 0;
}